import { WereWolfModule, WereWolfWinDb } from "@/werewolf/module";
import { WereWolfRole } from "../player";

export class WereWolfEndState extends WereWolfModule.State {
    override onEnter(): void {
        this.runner.run(async (r) => {
            await r.wait(20);
            if (this.context.winner === "villagers") {
                this.context.players.title("§a§l好人胜利");
            } else if (this.context.winner === "werewolves") {
                this.context.players.title("§c§l狼人已控场");
            } else {
                this.context.players.title("§e§l平局！");
            }
            this.addScore();
            await r.wait(60);
            this.engine.stopGame();
        });
    }

    /**加分 */
    addScore() {
        this.context.players.forEach((p) => {
            if (!p.role) return;
            if (
                this.context.winner == "werewolves" &&
                p.role == WereWolfRole.Werewolf
            ) {
                WereWolfWinDb.add(p.player, 1);
            } else if (
                this.context.winner == "villagers" &&
                [
                    WereWolfRole.Villager,
                    WereWolfRole.Hunter,
                    WereWolfRole.Seer,
                    WereWolfRole.Witch,
                ].includes(p.role)
            ) {
                WereWolfWinDb.add(p.player, 1);
            }
        });
    }
}
